<?php
session_start();
if(!defined('ROOT_DIR')) {
	define('ROOT_DIR', __DIR__ . '/');
}
if ($_POST) {
	$user = isset($_POST['user']) ? safe_replace($_POST['user']) : '';
	$room_id = isset($_POST['room_id']) ? intval($_POST['room_id']) : 1;
	if (empty($user)) {
		header('Content-Type: image/jpeg'); 
		echo file_get_contents('./ghost.gif');
	} else {
		try{
			$redis = new Redis();
			$redis->connect('127.0.0.1','6397');
		} catch(\Exception $e) {
			exit('连接redis 失败');
		}
		$redis_key = md5($user);
		$redis_hash = sprintf('swooleChat:user:%s:room:%d', $redis_key, $room_id);
		$redis->hset($redis_hash, 'user_name', $user);

		$_SESSION['isLogin'] = true;
		$_SESSION['userName'] = $user;
		$userInfo = $_SESSION['userInfo'] = $redis_key;

		include ROOT_DIR . 'say.html';
	}
	exit;
}

// 安全过滤函数
function safe_replace($string) {

	$string = str_replace('%20','',$string);
	$string = str_replace('%27','',$string);
	$string = str_replace('%2527','',$string);
	$string = str_replace('*','',$string);
	$string = str_replace('"','&quot;',$string);
	$string = str_replace("'",'',$string);
	$string = str_replace('"','',$string);
	$string = str_replace(';','',$string);
	$string = str_replace('<','&lt;',$string);
	$string = str_replace('>','&gt;',$string);
	$string = str_replace("{",'',$string);
	$string = str_replace('}','',$string);

	return str_replace('\\','',$string);
}

include ROOT_DIR . 'login.html';
